﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TabletopWidgetTestApp
{
    /// <summary>
    /// Interaction logic for ZoomMenu.xaml
    /// </summary>
    public partial class ZoomMenu : Grid
    {
        const float ZOOM_SCALAR = 0.10f;
        const float ZOOM_DEFAULT = 1.0f;
        private float _currentScale = 1.0f;

        public ZoomMenu()
        {
            InitializeComponent();
        }

        public void SetPosition(double x, double y, double actualWidth, double actualHeight)
        {
            Margin = new Thickness(x,y, actualWidth - x, actualHeight - y);
        }


        private void ZoomIn(object sender, RoutedEventArgs e)
        {
            Visibility = System.Windows.Visibility.Collapsed;

            _currentScale += _currentScale * ZOOM_SCALAR;

            _scaleSiblings();
        }


        private void ZoomOut(object sender, RoutedEventArgs e)
        {
            Visibility = System.Windows.Visibility.Collapsed;

            _currentScale -= _currentScale * ZOOM_SCALAR;

            _scaleSiblings();
        }


        private void zoomResetButton_Click(object sender, RoutedEventArgs e)
        {
            Visibility = System.Windows.Visibility.Collapsed;

            _currentScale = ZOOM_DEFAULT;

            _scaleSiblings();
        }


        private void _scaleSiblings()
        {
            // adjust the newX and newY so the zoom doesn't displace the user
            double newX = Mouse.GetPosition(this).X - Mouse.GetPosition(this).X * _currentScale;
            double newY = Mouse.GetPosition(this).Y - Mouse.GetPosition(this).Y * _currentScale;

            // build the transform matrices for this zoom
            TranslateTransform pan = new TranslateTransform(newX, newY);
            ScaleTransform scale = new ScaleTransform(_currentScale, _currentScale);

            // apply the transforms to a transform group
            TransformGroup g = new TransformGroup();
            //g.Children.Add(pan);
            g.Children.Add(scale);

            // get the full parent of this zoom menu
            Window parentWindow = Window.GetWindow(this);

            // This will always grab the layout children first (so grid, canvas, etc)
            foreach (FrameworkElement layoutChild in LogicalTreeHelper.GetChildren(parentWindow))
            {
                // get all the children within the layout
                foreach (FrameworkElement widgetChild in LogicalTreeHelper.GetChildren(layoutChild))
                {                    
                    Console.WriteLine(widgetChild.ToString());
                    //widgetChild.RenderTransform = g;
                    widgetChild.LayoutTransform = g;

                    widgetChild.UpdateLayout();
                }

                layoutChild.UpdateLayout();
            }

            parentWindow.UpdateLayout();
        }
    }
}
